import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
	login,
	logout,
	getInfo,
} from '@/api/login'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'
import * as base64 from "base-64"
import * as utf8 from "utf8"


const baseUrl = config.baseUrl


const user = {
	state: {
		//token保存store中
		dataList: null,
		data1: null,
		token: getToken(),
		name: storage.get(constant.name),
		avatar: storage.get(constant.avatar),
		roles: storage.get(constant.roles),
		permissions: storage.get(constant.permissions)
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_NAME: (state, name) => {
			state.name = name
			storage.set(constant.name, name)
		},
		SET_DATA: (state, dataList) => {
			state.dataList = dataList
			// storage.set(constant.name, name)
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
			storage.set(constant.avatar, avatar)
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles
			storage.set(constant.roles, roles)
		},
		SET_PERMISSIONS: (state, permissions) => {
			state.permissions = permissions
			storage.set(constant.permissions, permissions)
		}
	},

	actions: {
		// 登录
		Login({
			commit
		}, userInfo) {
			const username = userInfo.username.trim()
			const password = base64.encode(utf8.encode(userInfo.password))
			const clientId = userInfo.clientId
			const platformCode = userInfo.platformCode

			return new Promise((resolve, reject) => {
				// console.log(password)
				login(username, password, clientId, platformCode).then(res => {
					// console.log("res哈哈哈哈哈", res.data.token)
					// console.log("res哈哈哈哈哈")
					//token保存本地
					setToken(res.data.token)
					//token关联给mutations
					commit('SET_TOKEN', res.data.token)
					commit('SET_DATA', res.data)
					resolve(res)
				}).catch(error => {
					// console.log(error)
					reject(error)

				})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getInfo().then(res => {
					console.log("用户信息", res)
					const user = res.user
					console.log("cyuahc", user)
					const avatar = (user == null || user.avatar == "" || user.avatar == null) ?
						require("@/static/images/profile.jpg") : baseUrl + user.avatar
					const username = (user == null || user.userName == "" || user.userName ==
						null) ? "" : user.userName
					if (res.roles && res.roles.length > 0) {
						commit('SET_ROLES', res.roles)
						commit('SET_PERMISSIONS', res.permissions)
					} else {
						commit('SET_ROLES', ['ROLE_DEFAULT'])
					}
					commit('SET_NAME', username)
					commit('SET_AVATAR', avatar)
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				logout(state.token).then(() => {
					//设置 mutations中操作的token为空
					commit('SET_TOKEN', '')
					// 设置 角色为空
					commit('SET_ROLES', [])
					// 设置 权限permissions为空
					commit('SET_PERMISSIONS', [])
					//清除token
					removeToken()
					// 从本地缓存中同步移除指定的KEY
					storage.clean()
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}
	}
}

export default user
